﻿namespace BEGDC.CC.Web.UserControls.OutCR
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Collections;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using BEGDC.CC.BL.CCFlow.CR;
	using BEGDC.CC.BL.CCFlow.CV;
	using BEGDC.CC.BL.UserManagement;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.CC.BL.Common.Query;
	using BEGDC.Utility.Castle;

	/// <summary>
	///		Summary description for NormalRequest.
	/// </summary>
	public class Request : BaseOutCR
	{
		#region Variables
		protected System.Web.UI.WebControls.ImageButton Btn_Submit;
		protected System.Web.UI.WebControls.ImageButton Btn_Save;
		protected System.Web.UI.WebControls.ImageButton Btn_Reply;
		protected string _Branch = "";
		protected System.Web.UI.WebControls.TextBox tbOperationFlag1;
		protected System.Web.UI.WebControls.TextBox tbDepartment;
		protected System.Web.UI.WebControls.ImageButton Btn_Copy;
		protected System.Web.UI.WebControls.Label tx_AppState;
		protected System.Web.UI.HtmlControls.HtmlTableRow tr_Title;
		protected BEGDC.CC.Web.UserControls.OutCR.EZCtrlForCO EZCtrlForCO;
		protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenScrollHight;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.HtmlControls.HtmlGenericControl SampleCO;
		protected bool _Transition = false;
		bool isReadOnly = false;

		bool saveBeforeSubmit=false;//joan 2008/8/22
		private bool IsView
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				if(state == SysDicItem.ProjectStatusFlag.View || state == SysDicItem.ProjectStatusFlag.CANCEL
					|| state == SysDicItem.ProjectStatusFlag.Pending || state == SysDicItem.ProjectStatusFlag.End
					|| state == SysDicItem.ProjectStatusFlag.Participation)
					return true;
				else
					return false;
			}
		}
		#endregion

		private bool IsQuery
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				return state == SysDicItem.ProjectStatusFlag.QUERY;
			}
		}

		protected string Step
		{
			get
			{
				IQuery queryService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
				FlowInstance flowInstance = queryService.QueryFlowInstance( this.AppID );
				if( flowInstance == null )
					return BEGDC.CC.BL.Workflow.WorkTransition.Draft;
				else
					return flowInstance.CurrentActiveStep.StepCode;
			}
		}

		private void Page_Load(object sender, System.EventArgs e)
		{			
			if(!this.IsPostBack)
			{
				//John Add 8/12
				InValidAllSession();


				base.PreInitForm();

				BuildFormProperties();

				//this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
				this.Btn_Submit.Attributes["onclick"] = "javascript:OpenAssignmentPane();";
				this.Btn_Copy.Attributes["onclick"] = "javascript:OnCopy();";
				this.Btn_Reply.Attributes.Add("onclick", "javascript:OpenReplyPane("+Request.QueryString["FlowID"]+");");
				ButtonStatusSetting();

				InitForm();

			}
//			Page.SmartNavigation = true;

			if (!( ((this.Step == BEGDC.CC.BL.Workflow.WorkTransition.CO_Request) || (this.Step == BEGDC.CC.BL.Workflow.WorkTransition.Draft) ||
				(this.Step == BEGDC.CC.BL.Workflow.WorkTransition.CO_ReRequest)) && (!this.IsQuery) 
				&& (Request.Url.AbsoluteUri.IndexOf("workflowview") < 0)))
			{
				this.Btn_Save.Visible = false;
				this.Btn_Submit.Visible = false;
				this.Btn_Copy.Visible = false;
			}

		}
		
		#region Form Properties
		protected string Department
		{
			get{ return (string)this.ViewState["Department"]; }
			set{ this.ViewState["Department"] = value; }
		}
		protected string Branch
		{
			get{ return (string)this.ViewState["Branch"]; }
			set{ this.ViewState["Branch"] = value; }
		}

		#endregion

		#region InitForm
		private void InitForm()
		{
			long flowId  = this.LoadApplication();

			if(this.Application!=null)
			{
				//UPDATE FORM PROPERTIES IF IT IS QUERY BACK
				if(this.Application.DepartmentType!=null)
				{
					this.Department = this.Application.DepartmentType.DicItemKey;
				}
				if(this.Application.DepartmentName!=null)
				{
					this.Branch = this.Application.DepartmentName.Id.ToString();
				}
				//this.Transition = this.Application.IsTransition;

				//Display Copy Button
				if(flowId==0)
				{
					this.Btn_Copy.Visible = true;
				}

				EZCtrlForCO.Visible = true;
				EZCtrlForCO.isReadOnly = isReadOnly;
				if( this.Step == BEGDC.CC.BL.Workflow.WorkTransition.End )
				{
					EZCtrlForCO.KnowledgeVisible = true;
				}
				else
				{
					EZCtrlForCO.KnowledgeVisible = false;
				}
			}
			else if(this.AppID==0&&this.Application==null)
			{
				EZCtrlForCO.Visible = true;
				EZCtrlForCO.isReadOnly = isReadOnly;
			}
			else
			{
				WebUtility.Go2ErrorPage( this.Page, "wrong Application ID" );
			}

			if(this.IsView)
			{
				this.tr_Title.Visible = false;
			}
		}
		#endregion

		#region Build Form Properties
		private void BuildFormProperties()
		{
			IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
			Employee CurrentRequestor = userService.GetUser(HttpContext.Current.User.Identity.Name);

			if(CurrentRequestor.Department!=null)
			{
				this.Branch = CurrentRequestor.Department.Id.ToString();
				this.Department = CurrentRequestor.Department.DepartmentType.DicItemKey;
				this.tbDepartment.Text = this.Branch + "_" + this.Department ;
			}

			string TranStr = Request.QueryString["Transition"];

		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Btn_Reply.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Reply_Click);
			this.Btn_Save.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Save_Click);
			this.Btn_Submit.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Submit_Click);
			this.Btn_Copy.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Copy_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region BuildObjects

		private DataTable BuildSessionTable(string SessionName)
		{
			DataTable dataTable = null;
			object SessDataTable = Session[SessionName];
			if(SessDataTable!=null)
			{
				dataTable = (DataTable)SessDataTable;
			}
			return dataTable;
		}

		private void InValidAllSession()
		{
			Session[SessionFlag.AccompanySession] = null;
			Session[SessionFlag.CustomerSession] = null;
			Session[SessionFlag.AccompanySessionEZ] = null;
			Session[SessionFlag.CustomerSessionEZ] = null;



			Session[SessionFlag.CompanyCarSession] = null;
			Session[SessionFlag.CompanyCarSessionEZ] = null;
			Session[SessionFlag.LeaseCarSession] = null;
			Session[SessionFlag.LeaseCarSessionEZ] = null;
			Session[SessionFlag.HotelSession] = null;
			Session[SessionFlag.HotelSessionEZ] = null;

			Session[SessionFlag.ContractInfo] = null;
			Session[SessionFlag.CostCenter] = null;

		}

		#endregion

		#region Save
		private void Btn_Save_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			string url = Request.Form["tbURL"];
			string script = "<script>";
			try
			{
				//OBTAIN BASIC INFORMATION
				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
				BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);

				Application app = new Application();
				if(this.Application!=null)
				{
					app.Id = this.AppID;
					app.AppNo = this.Application.AppNo;
					app.IsTransition = this.Application.IsTransition;
					app.DepartmentType = this.Application.DepartmentType;
					app.DepartmentName = this.Application.DepartmentName;
				}
				if(app.DepartmentType==null&&this.Department!="")
				{
					app.DepartmentType = utility.GetDicItem(this.Department,  BEGDC.CC.BL.Common.DicType.DepartmentType);
					app.IsTransition = true;
				}
				if(app.DepartmentName==null&&this.Branch!=null&&this.Branch!="")
				{
					app.DepartmentName = new Department();
					app.DepartmentName.Id = Convert.ToInt32(this.Branch);
				}

				CustomerInfo customer = new CustomerInfo();
				BusinessPurpose business = new BusinessPurpose();
				Topic topic = new Topic();
				SalesTeamInfo salesTeam = new SalesTeamInfo();
				LogisticalInfo logist = new LogisticalInfo();
				PaymentInfo payment = new PaymentInfo();
				IList customerAttendee = new ArrayList();
				IList alAttendee = new ArrayList();
				IList site = new ArrayList();
				IList cost = new ArrayList();
				IList contract = new ArrayList();
				//update Jovi
				DataTable HotelTable = null;
				DataTable LeaseCarTable = null;

				EZCtrlForCO.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract, ref HotelTable, ref LeaseCarTable);
				app.ApplicationType = WorkflowTypes.CO;
				CRService.SaveEZApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost, contract, HotelTable, null, LeaseCarTable, false, false);

				this.AppID = app.Id;
				this.Application = app;
				EZCtrlForCO.CustomerID = customer.Id;
				EZCtrlForCO.TopicID = topic.Id;
				EZCtrlForCO.SalesTeamID = salesTeam.Id;
				EZCtrlForCO.LogistID = logist.Id;
				EZCtrlForCO.BusinessID = business.Id;
				EZCtrlForCO.PaymentID = payment.Id;



				Session["AppID"] = this.Application.Id;

				if(url == string.Empty)
					script += "VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');";
				if(url != string.Empty)
				{
					if(url.IndexOf("AppID") == -1)
						url = url.Substring(0,url.IndexOf("?")+1)+"AppID="+app.Id+"&"+url.Substring(url.IndexOf("?")+1);
					script += "window.location.href='"+url+"'";
				}
				else
				{
					url = Request.Url.AbsoluteUri;
					if(url.IndexOf("AppID") == -1)
						url = url.Substring(0,url.IndexOf("?")+1)+"AppID="+app.Id+"&"+url.Substring(url.IndexOf("?")+1);
					script += "window.location.href='"+url+"'";
				}
				script += "</script>";

				this.Btn_Copy.Visible = true;
				this.EZCtrlForCO.ShowExport();
			}
			catch(Exception exp)
			{
				//throw exp;
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
			//<--joan 2008/8/22
			if (!this.saveBeforeSubmit)
			{
				Page.RegisterClientScriptBlock("",script);
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');</script>");
			}
				
			//-->end 
		}
		#endregion

		#region Submit
		private void Btn_Submit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				string msg;
				this.saveBeforeSubmit=true;
				this.Btn_Save_Click(this.Btn_Save,null);
				this.saveBeforeSubmit=false;

//				IList attTypes = new ArrayList();
//				attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.AGENDA);
//				attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.BUDGET);
//				if (this.AppID<=0)
//				{
//					msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID, attTypes );
//				}
//				else
//				{
//					msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID);
//				}

				msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID);
				if( msg!="" && msg!=string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + msg + "')</script>");
					return;
				}
				Implement_Submit();
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				//throw exp;
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
			finally
			{
				//InValidAllSession();
			}
		}

		private void Implement_Submit()
		{
			try
			{
				//OBTAIN BASIC INFORMATION
				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
				//BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);

				if(!CheckFormValidation())
				{
					return;
				}

				if(this.EZCtrlForCO.IsSomeoneElse&&this.EZCtrlForCO.SomeoneEmployee==null)
				{
					Page.RegisterClientScriptBlock("","<script>alert('Please specify some one else you are delegating for');</script>");
					return;
				}
				if(this.EZCtrlForCO.IsSomeoneElse&&this.EZCtrlForCO.SomeoneEmployee!=null)
				{
					BEGDC.CC.Entity.Employee Someone = this.EZCtrlForCO.SomeoneEmployee;
					this.Branch = Someone.Department.Id.ToString();
					this.Department = Someone.Department.DepartmentType.DicItemKey;
				}

				Application app = new Application();
				if(this.Application!=null)
				{
					app.Id = this.AppID;
					app.AppNo = this.Application.AppNo;
					if(app.AppNo.IndexOf("-D")>-1)
						app.AppNo=string.Empty;
					app.IsTransition = this.Application.IsTransition;
					app.DepartmentType = this.Application.DepartmentType;
					app.DepartmentName = this.Application.DepartmentName;
				}
//				if(app.DepartmentType==null&&this.Department!="")
//				{
//					app.DepartmentType = utility.GetDicItem(this.Department,  BEGDC.CC.BL.Common.DicType.DepartmentType);
//					app.IsTransition = true;
//				}
//				if(app.DepartmentName==null&&this.Branch!=null&&this.Branch!="")
//				{
//					app.DepartmentName = new Department();
//					app.DepartmentName.Id = Convert.ToInt32(this.Branch);
//				}

				CustomerInfo customer = new CustomerInfo();
				BusinessPurpose business = new BusinessPurpose();
				Topic topic = new Topic();
				SalesTeamInfo salesTeam = new SalesTeamInfo();
				LogisticalInfo logist = new LogisticalInfo();
				PaymentInfo payment = new PaymentInfo();
				IList customerAttendee = new ArrayList();
				IList alAttendee = new ArrayList();
				IList site = new ArrayList();
				IList cost = new ArrayList();
				IList contract = new ArrayList();
				//update Jovi
				DataTable HotelTable = null;
				DataTable LeaseCarTable = null;

				EZCtrlForCO.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract, ref HotelTable, ref LeaseCarTable);
				app.ApplicationType = WorkflowTypes.CO;

				BindAppDepartment(ref app);
				string catDirector = string.Empty ;
				if( Session["CATDirector"] != null )
				{
					catDirector =  Session["CATDirector"].ToString();
				}
				CRService.SubmitEZApplicationForPSO(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost,contract,HotelTable,null, LeaseCarTable, null, string.Empty, false, catDirector);

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				//throw exp;
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
		}

		private void BindAppDepartment(ref BEGDC.CC.Entity.Application app)
		{
			IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
			if(app.SomeoneElseNo!=null)
			{
				app.DepartmentType = utility.GetDicItem(app.SomeoneElseNo.Department.DepartmentType.DicItemKey,  BEGDC.CC.BL.Common.DicType.DepartmentType);
				app.IsTransition = true;
				app.DepartmentName = new Department();
				app.DepartmentName.Id = Convert.ToInt32(app.SomeoneElseNo.Department.Id);
			}
			else
			{
				if(app.DepartmentType==null&&this.Department!="")
				{
					app.DepartmentType = utility.GetDicItem(this.Department,  BEGDC.CC.BL.Common.DicType.DepartmentType);
					app.IsTransition = true;
				}
				if(app.DepartmentName==null&&this.Branch!=null&&this.Branch!="")
				{
					app.DepartmentName = new Department();
					app.DepartmentName.Id = Convert.ToInt32(this.Branch);
				}
			}
		}

		private bool CheckFormValidation()
		{
			if(this.EZCtrlForCO.CustomerNum==0)
			{
				Page.RegisterClientScriptBlock("","<script>alert('At least one customer should be specified.');</script>");
				return false;
			}
			if(this.EZCtrlForCO.AccompanyNum==0)
			{
				Page.RegisterClientScriptBlock("","<script>alert('At least one Alcatel-Lucent attendee should be specified.');</script>");
				return false;
			}
//			if(this.EZCtrlForCO.HotelNum==0)
//			{
//				Page.RegisterClientScriptBlock("","<script>alert('At least one Hotel should be specified.');</script>");
//				return false;
//			}
			if(this.EZCtrlForCO.CostCenterNum==0)
			{
				Page.RegisterClientScriptBlock("","<script>alert('At least one cost center code should be specified.');</script>");
				return false;
			}
		
			return true;
		}
		#endregion

		#region Copy
		private void Btn_Copy_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
				CRService.CopyCR(this.AppID);
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBalert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
				//throw exp;
			}
			Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM004 + "');</script>");
//			try
//			{
//				//OBTAIN BASIC INFORMATION
//				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
//				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
//
//				Application app = new Application();
//				if(this.Application!=null)
//				{
//					app.IsTransition = this.Application.IsTransition;
//					app.DepartmentType = this.Application.DepartmentType;
//					app.DepartmentName = this.Application.DepartmentName;
//				}
//				if(app.DepartmentType==null&&this.Department!="")
//				{
//					app.DepartmentType = utility.GetDicItem(this.Department,BEGDC.CC.BL.Common.DicType.DepartmentType);
//					app.IsTransition = true;
//				}
//				if(app.DepartmentName==null&&this.Branch!=null&&this.Branch!="")
//				{
//					app.DepartmentName = new Department();
//					app.DepartmentName.Id = Convert.ToInt32(this.Branch);
//				}
//
//				CustomerInfo customer = new CustomerInfo();
//				BusinessPurpose business = new BusinessPurpose();
//				Topic topic = new Topic();
//				SalesTeamInfo salesTeam = new SalesTeamInfo();
//				LogisticalInfo logist = new LogisticalInfo();
//				PaymentInfo payment = new PaymentInfo();
//				IList customerAttendee = new ArrayList();
//				IList alAttendee = new ArrayList();
//				IList site = new ArrayList();
//				IList cost = new ArrayList();
//				IList contract = new ArrayList();
//				//update Jovi
//				DataTable CompanyCarTable = null;
//				DataTable LeaseCarTable = null;
//
//				EZCtrlForCO.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract, ref CompanyCarTable, ref LeaseCarTable);
//				app.ApplicationType = WorkflowTypes.CO;
//				CRService.SaveEZApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost,contract,null,CompanyCarTable, LeaseCarTable, true, false);
//
//				this.AppID = app.Id;
//				this.Application = app;
//				this.tx_AppState.Text = "Copy of  "+ app.AppNo;
//				this.tx_AppState.Visible = true;
//			}
//			catch(Exception exp)
//			{
//				throw exp;
//				//Page.RegisterClientScriptBlock("","<script>alert('Copy failed.');</script>");
//			}
//
//			Page.RegisterClientScriptBlock("","<script>alert('Copy itself successfully, You can work on this copy now.');</script>");
		}

		private DataTable InitializeDataTable(System.Data.DataTable dataTable)	
		{
			DataRow dataRow;
			for(int i=0;i<dataTable.Rows.Count;i++)
			{
				dataRow = dataTable.Rows[i];
				dataRow[0] = 0;
			}
			return dataTable;
		}

		#endregion

		#region ButtonStatusSetting
		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.Btn_Submit.Visible = this.Access.IsButtonVisible(COTransitionList.Submit);
			this.Btn_Save.Visible = this.Access.IsButtonVisible(COTransitionList.Save);
			this.Btn_Reply.Visible = this.Access.IsButtonVisible(COTransitionList.Reply);
			this.Btn_Copy.Visible = this.Access.IsButtonVisible(COTransitionList.Copy);

			if( ((this.Access.IsHideAllButton || this.Access.IsReadOnly) || (this.Step != WorkTransition.Draft
				&& this.Step != WorkTransition.CO_Request && this.Step != WorkTransition.CO_ReRequest)) && !this.IsQuery)
				tr_Title.Visible = false;

			if((this.Access.IsReadOnly || this.IsView))
				isReadOnly = true;
		}
		#endregion

		#region Reply
		private void Btn_Reply_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
				BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);

				Application app = new Application();
				if(this.Application!=null)
				{
					app.Id = this.AppID;
					app.AppNo = this.Application.AppNo;
					app.IsTransition = this.Application.IsTransition;
					app.DepartmentType = this.Application.DepartmentType;
					app.DepartmentName = this.Application.DepartmentName;
				}
				if(app.DepartmentType==null&&this.Department!="")
				{
					app.DepartmentType = utility.GetDicItem(this.Department,  BEGDC.CC.BL.Common.DicType.DepartmentType);
					app.IsTransition = true;
				}
				if(app.DepartmentName==null&&this.Branch!=null&&this.Branch!="")
				{
					app.DepartmentName = new Department();
					app.DepartmentName.Id = Convert.ToInt32(this.Branch);
				}

				CustomerInfo customer = new CustomerInfo();
				BusinessPurpose business = new BusinessPurpose();
				Topic topic = new Topic();
				SalesTeamInfo salesTeam = new SalesTeamInfo();
				LogisticalInfo logist = new LogisticalInfo();
				PaymentInfo payment = new PaymentInfo();
				IList customerAttendee = new ArrayList();
				IList alAttendee = new ArrayList();
				IList site = new ArrayList();
				IList cost = new ArrayList();
				IList contract = new ArrayList();
				//update Jovi
				DataTable HotelTable = null;
				DataTable LeaseCarTable = null;

				EZCtrlForCO.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract, ref HotelTable, ref LeaseCarTable);
				app.ApplicationType = WorkflowTypes.CO;
				CRService.SaveEZApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost, contract, HotelTable, null, LeaseCarTable, false, false);

				CRService.COReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, tbComment.Text));

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				//throw exp;
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
		}
		#endregion

		#region Modify
		public void ShowModifyStatus()
		{
			this.EZCtrlForCO.ShowModifyStatus();
		}

		public void EndModifyStatus()
		{
			this.EZCtrlForCO.EndModifyStatus();
		}

		public void Modify()
		{
			ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
			BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);
			if(App!=null)
			{
				IList customerAttendee = new ArrayList();
				IList alAttendee = new ArrayList();
				EZCtrlForCO.BuildModificationChange(ref App,ref customerAttendee,ref alAttendee);
				CRService.SaveEZModification(App,customerAttendee,alAttendee);
			}
		}
		#endregion
	}
}
